package com.hxd.otc.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.constraint.ConstraintLayout;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.TextView;

import com.hxd.otc.R;
import com.hxd.otc.module.my.activity.WebViewActivity;
import com.hxd.otc.module.my.bean.NoticeBean;
import com.hxd.otc.utils.DateUtil;
import com.hxd.otc.utils.GlideUtils;
import com.hxd.otc.utils.UIUtils;
import com.hxd.otc.widget.AutoSplitOnTvGlobalLayoutAdapter;

/**
 * 作  者 :  胡庆岭
 * 时  间 :  2018/6/6 19:44
 * 描  述 :  ${但点击按钮Dialog 如果不要内容栏，不设定内容文字就好}
 */
public class SystemNoticeDialog extends Dialog implements View.OnClickListener {

    private TextView mTitleText;
    private TextView mContentText;
    private Button mGotItText;
    private ImageView ivClose;
    private ConstraintLayout cl_content;
    private ImageView iv_img;
    private ScrollView sl_content;
    private TextView tvDate;

    NoticeBean noticeBean;

    public void setNoticeBean(NoticeBean noticeBean) {
        this.noticeBean = noticeBean;

        if (TextUtils.isEmpty(noticeBean.getPictureUrl())) {// 不显示图片，显示文本
            cl_content.setVisibility(View.VISIBLE);
            iv_img.setVisibility(View.GONE);
            mTitleText.setText(noticeBean.getTitle());
            mContentText.setText(noticeBean.getContent());
            tvDate.setText(DateUtil.format(DateUtil.parseToDate(noticeBean.getCreateTime(), DateUtil.YY_MM_DD_HH_MM_SS), "yyyy年MM月dd日"));
            if (!TextUtils.isEmpty(noticeBean.getUrl())) {
                mGotItText.setText(getContext().getString(R.string.look_detail));
            }
        } else {//显示图片
            cl_content.setVisibility(View.GONE);
            iv_img.setVisibility(View.VISIBLE);
            GlideUtils.loadNormal(getContext(), UIUtils.getServiceImageUrlByPath(noticeBean.getPictureUrl()), iv_img);
        }
    }

    public void setOnSingClickListener(OnSingClickListener onSingClickListener) {
        this.onSingClickListener = onSingClickListener;
    }

    private OnSingClickListener onSingClickListener;


    public SystemNoticeDialog(@NonNull Context context) {
        this(context, R.style.BlockDialog);

    }

    public SystemNoticeDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.system_notice_dialog);
        initView();
        initWindows();
    }


    private void initView() {
        mTitleText = findViewById(R.id.tv_title);
        mContentText = findViewById(R.id.content_tv);
        mGotItText = findViewById(R.id.got_it_tv);
        ivClose = findViewById(R.id.iv_close);
        iv_img = findViewById(R.id.iv_img);
        cl_content = findViewById(R.id.cl_content);
        sl_content = findViewById(R.id.sl_content);
        tvDate = findViewById(R.id.tv_date);
        mGotItText.setOnClickListener(this);
        ivClose.setOnClickListener(this);
        iv_img.setOnClickListener(this);
        mContentText.getViewTreeObserver().addOnGlobalLayoutListener(new AutoSplitOnTvGlobalLayoutAdapter(mContentText));
    }

    private void initWindows() {
        setCanceledOnTouchOutside(false);
        Window window = getWindow();
        if (window != null) {
            DisplayMetrics dm = new DisplayMetrics();
            window.getWindowManager().getDefaultDisplay().getMetrics(dm);
            WindowManager.LayoutParams windowAttributes = window.getAttributes();
            windowAttributes.width = WindowManager.LayoutParams.MATCH_PARENT;
            windowAttributes.height = WindowManager.LayoutParams.WRAP_CONTENT;
            windowAttributes.gravity = Gravity.CENTER;
            window.setAttributes(windowAttributes);
        }
    }

    @Override
    public void show() {
        ivClose.setVisibility(View.VISIBLE);
        sl_content.setVisibility(TextUtils.isEmpty(mContentText.getText()) ? View.GONE : View.VISIBLE);
        super.show();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_close:
                dismiss();
                if (onSingClickListener != null) {
                    onSingClickListener.singItemClick();
                }
                break;
            case R.id.got_it_tv:
                dismiss();
                if (TextUtils.isEmpty(noticeBean.getUrl())) {// 纯文本，不做跳转
                    if (onSingClickListener != null) {
                        onSingClickListener.singItemClick();
                    }
                } else {
                    if (!TextUtils.isEmpty(noticeBean.getUrl())) {
                        Intent intent = new Intent();
                        Bundle bundle = new Bundle();
                        bundle.putString(WebViewActivity.EXTRA_TITLE, noticeBean.getTitle());
                        bundle.putString(WebViewActivity.EXTRA_URL, noticeBean.getUrl());
                        intent.putExtras(bundle);
                        intent.setClass(getContext(), WebViewActivity.class);
                        getContext().startActivity(intent);
                    }
                }

                break;
            case R.id.iv_img:// 有图片，且有链接
                if (!TextUtils.isEmpty(noticeBean.getUrl())) {
                    Intent intent = new Intent();
                    Bundle bundle = new Bundle();
                    bundle.putString(WebViewActivity.EXTRA_TITLE, noticeBean.getTitle());
                    bundle.putString(WebViewActivity.EXTRA_URL, noticeBean.getUrl());
                    intent.putExtras(bundle);
                    intent.setClass(getContext(), WebViewActivity.class);
                    getContext().startActivity(intent);
                    dismiss();
                }
                break;
        }

    }

    public interface OnSingClickListener {
        void singItemClick();
    }


}
